let accountMap = new Map();
let ACCOUNT_TYPE_ALI = 1;
let ACCOUNT_TYPE_TX = 2;
let TX_ACCOUNT = "腾讯云账号";
layui.use(['jquery','element','layer','form'], function(){
    var table = layui.table,
        $ = layui.jquery,
        layer = layui.layer;
    var form=layui.form;

    // 获取Ak配置信息
    initAkInfo();

    function initAkInfo() {
        $.get('/cdsmsmgr/smsAkInfo/getAkInfoByUser', {}, function (result) {
            if(result != null){
                if(result.resultContent != null){
                    var akInfo = result.resultContent;
                    setAkInfo(akInfo);
                }
            }
        });
    }
    function setAkInfo(akInfo){
        if(akInfo){
            $("#akInfoId").val(akInfo.id);
            let type = akInfo.thirdAccountType;
            $("#thirdAccountType").val(type);
            $("#akId").val(akInfo.akId);
            $("#akSecret").val(akInfo.akSecret);
            if(type == TX_ACCOUNT){
                $("#appid").val(akInfo.appid);
                $("#appidDiv").show();
                accountMap.set(ACCOUNT_TYPE_TX,akInfo);
            }else {
                $("#appid").val("");
                $("#appidDiv").hide();
                accountMap.set(ACCOUNT_TYPE_ALI,akInfo);
            }
            form.render();
        }
    }
    // 保存
    form.on('submit(saveAkSetting)',function(data){
        let type = $("#thirdAccountType").val();
        let appid = $("#appid").val();
        if(type == TX_ACCOUNT && !appid){
            layer.msg("请输入SDK AppId", {icon: 0});
            return false;
        }
        $.post("/cdsmsmgr/smsAkInfo/saveAkInfo",data.field, function(result) {
            if(result.resultCode === '000' ){
                layer.msg('保存成功', {icon: 1});
                setAkInfo(result.resultContent);
                setTimeout(function () {
                    $(window).attr('location','templateSetting.html');
                },800);
            }else{
                layer.msg(result.resultDesc, {icon: 0});
            }
        });
        return false;
    });

    form.on('select(thirdAccountType)',function (data) {
        let val = data.value;
        let accountInfo;
        if(val == TX_ACCOUNT){
            $("#appidDiv").show();
            accountInfo = accountMap.get(ACCOUNT_TYPE_TX);
        }else {
            $("#appidDiv").hide();
            accountInfo = accountMap.get(ACCOUNT_TYPE_ALI);
        }
        if(accountInfo){
            $("#akId").val(accountInfo.akId);
            $("#akSecret").val(accountInfo.akSecret);
            $("#appid").val(accountInfo.appid);
        }else {
            $("#akId").val("");
            $("#akSecret").val("");
            $("#appid").val("");
        }
        return false;
    });

    function setInfo(){
        let akId = $("#akId").val();
        let akSecret = $("#akSecret").val();
        let appid = $("#appid").val();
        let type = $("#thirdAccountType").val();
        let infoOBj = {akId:akId,akSecret:akSecret,appid:appid};
        if(TX_ACCOUNT == type){
            accountMap.set(ACCOUNT_TYPE_TX,infoOBj);
        }else {
            accountMap.set(ACCOUNT_TYPE_ALI,infoOBj);
        }
    }

    $("#akId, #akSecret, #appid").on('blur',function(){
        setInfo();
    });

});
